package com.fh.modules.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.constant.Constant;
import com.fh.common.exception.SelfDefinedException;
import com.fh.common.util.ConvertUtils;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.system.entity.SysUser;
import com.fh.modules.system.entity.SysUserRole;
import com.fh.modules.system.mapper.SysUserMapper;
import com.fh.modules.system.mapper.SysUserRoleMapper;
import com.fh.modules.system.service.ISysUserService;

/**
 * @author Qiu,Junda
 * @date 2019年4月3日
 * @description 用户表 服务实现类
 */
@Service
public class SysUserServiceImpl extends AbstractServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
	
	@Autowired
	private SysUserMapper userMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	

	@Override
//	@Cacheable(value="sysUser",key="#username")
	public SysUser getUserByName(String username) {
		return userMapper.getUserByName(username);
	}
	
	
	@Override
//	@CacheEvict(value="loginUser_cacheRules", allEntries=true)
	@Transactional
	public void addUserWithRole(SysUser user, String roles) {
		this.save(user);
		if(ConvertUtils.isNotEmpty(roles)) {
			String[] arr = roles.split(",");
			for (String roleId : arr) {
				SysUserRole userRole = new SysUserRole(user.getId(), roleId);
				sysUserRoleMapper.insert(userRole);
			}
		}
	}
	
	@Override
//	@CacheEvict(value="sysUser",beforeInvocation=true,allEntries=true)
	public void deleteUserById(String userId) {
		if(Constant.ADMIN_USER_ID.equals(userId))
			throw new SelfDefinedException("无法删除admin管理员账号");
		this.deleteEntityByDelFlag(userId);
	}

	@Override
//	@CacheEvict(value="sysUser",beforeInvocation=true,allEntries=true)
	public void editUserWithRole(SysUser user, String roles) {
		this.updateById(user);
		//先删后加
		sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId()));
		if(ConvertUtils.isNotEmpty(roles)) {
			String[] arr = roles.split(",");
			for (String roleId : arr) {
				SysUserRole userRole = new SysUserRole(user.getId(), roleId);
				sysUserRoleMapper.insert(userRole);
			}
		}
	}
	@Override
//	@CacheEvict(value="sysUser",beforeInvocation=true,allEntries=true)
	public void frozenBatch(String userId, int status) {
		if (Constant.ADMIN_USER_ID.equals(userId)) {
			throw new SelfDefinedException("无法对admin账号进行操作");
		}
		this.update(new SysUser().setStatus(status),
				new UpdateWrapper<SysUser>().lambda().eq(SysUser::getId,userId));
	}

	@Override
	public List<String> getRole(String userId) {
		return sysUserRoleMapper.getRoleByUserName(userId);
	}

	@Override
	public boolean isAdminRole(String userId) {
		return userMapper.isAdminRole(userId)>0?true:false;
	}
	
	// 根据部门Id查询
	@Override
	public IPage<SysUser> getUserByDepId(Page<SysUser> page, String departId,String username) {
		return userMapper.getUserByDepId(page, departId,username);
	}

}
